package net.xju3_4.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import net.xju3_4.common.framework.simplemvc.RequestMapping;
import net.xju3_4.common.utils.JSONUtil;
import net.xju3_4.common.utils.Result;
import net.xju3_4.entity.LoginRegisterEntity;
import net.xju3_4.service.LoginRegisterService;
import net.xju3_4.service.impl.LoginRegisterServiceImpl;

import java.io.UnsupportedEncodingException;

@RequestMapping("/api/user")
public class LoginRegisterController {
    LoginRegisterService userService = new LoginRegisterServiceImpl();

@RequestMapping("/userInfo.do")
public void userInfo(HttpServletRequest req, HttpServletResponse resp) {
    HttpSession session = req.getSession();
    Object sessionUser = session.getAttribute("user"); // 获取 session 中的用户对象

    if (sessionUser == null) {
        JSONUtil.printByJSON(resp, Result.fail("请登录"));
    } else if (sessionUser instanceof LoginRegisterEntity) {
        LoginRegisterEntity user = (LoginRegisterEntity) sessionUser; // 确保类型安全的转换
        JSONUtil.printByJSON(resp, Result.success(user, "查询成功"));
    } else {
        // 如果 session 中的用户对象不是 User 类型，返回错误信息
        JSONUtil.printByJSON(resp, Result.fail("用户类型不正确"));
    }
}
//车辆租赁登录
    @RequestMapping("/login.do")
    public void login(HttpServletRequest req, HttpServletResponse resp) {
        try {
            req.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String role = req.getParameter("role");
        LoginRegisterEntity user = userService.login(username, password, role);
        if(user != null){
            HttpSession session = req.getSession();
            session.setAttribute("user", user);
            JSONUtil.printByJSON(resp, Result.success(user,"登录成功"));
        }else{
            JSONUtil.printByJSON(resp, Result.fail("用户名或密码错误"));
        }
    }
//    新增数据
    @RequestMapping("/insert.do")
    public  void insert(HttpServletRequest req, HttpServletResponse resp) {

            LoginRegisterEntity user = new LoginRegisterEntity();
        try {
            req.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            JSONUtil.printByJSON(resp, Result.fail("字符编码设置失败"));
            return;
        }
        String msg;
        user.setRole(req.getParameter("role"));
        user.setUsername(req.getParameter("username"));
        user.setPassword(req.getParameter("password"));
        user.setPhone(req.getParameter("phone"));
        user.setRealName(req.getParameter("realName"));
        user.setIdCard(req.getParameter("idCard"));
        user.setAvatarUrl(req.getParameter("avatarUrl"));
        //商家
        user.setDriverLicenseEncrypted(req.getParameter("driverLicenseEncrypted"));
        user.setMerchantName(req.getParameter("merchantName"));
        user.setContactPerson(req.getParameter("contactPerson"));
        user.setContactPhone(req.getParameter("contactPhone"));
        user.setBusinessLicense(req.getParameter("businessLicense"));
        user.setLogoUrl(req.getParameter("logoUrl"));
        user.setAddress(req.getParameter("address"));
        if(user.getRole().equals("商家")){
            msg = "申请注册中，等待管理员审核";
        }else {
            msg = "注册成功";
        }
        if ( userService.insert( user)){

            JSONUtil.printByJSON(resp, Result.success(user, msg));
        }else {
            JSONUtil.printByJSON(resp, Result.fail("添加失败"));
        }
    }


}

